package com.zqq.baselibrary.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zqq.baselibrary.common.BaseApplication
import com.zqq.baselibrary.injection.component.ActivityComponent
import com.zqq.baselibrary.injection.component.DaggerActivityComponent
import com.zqq.baselibrary.injection.module.ActivityModule
import com.zqq.baselibrary.injection.module.LifecycleProviderModule
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.baselibrary.presenter.view.BaseView
import com.zqq.baselibrary.widgets.ProgressLoading
import org.jetbrains.anko.support.v4.toast
import org.jetbrains.anko.toast
import javax.inject.Inject

/**
 * Fragment基类，业务相关
 * @author zqq on 2018/7/26
 */
abstract class BaseMvpFragment<T : BasePresenter<*>> : BaseFragment(), BaseView {

    @Inject
    lateinit var mPresenter: T
    lateinit var mActivityComponent: ActivityComponent

    private lateinit var mLoadingDialog: ProgressLoading

    /**Dagger注册*/
    protected abstract fun injectComponent()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initActivityInjection()
        injectComponent()

        //初始化加载匡
        mLoadingDialog= ProgressLoading.create(context!!)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    /**
     * 实例化ActivityComponent
     */
    private fun initActivityInjection() {
        mActivityComponent = DaggerActivityComponent.builder()
                //因为ActivityCom依赖于AppCom所以需要AppCom实例，且其在BaseApplication已实例化
                .appComponent((activity!!.application as BaseApplication).appComponent)
                .activityModule(ActivityModule(activity!!))
                //因为BaseFragment继承了Rx**且实现了Life**，所以可以直接用this
                .lifecycleProviderModule(LifecycleProviderModule(this))
                .build()         //返回的的是ActivityComponent说明实例化ActivityComponent成功了
    }


    /**---------------------Loading------------------------*/
    /*-显示加载框，默认实现-*/
    override fun showLoading() {
        mLoadingDialog.showLoading()
    }

    /*-隐藏加载框，默认实现-*/
    override fun hideLoading() {
        mLoadingDialog.hideLoading()
    }

    /*-错误信息提示，默认实现-*/
    override fun onError(text: String) {
        toast(text)
    }


}